// pages/orgDetail/orgDetail.js
const app = getApp();
const util = require("../../utils/util.js");
import Toast from '../..//dist/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options:'',
    iswifi: false,
    top:0,
    fixedNav:false,
    isParentOrgRw:null,
    imgpopup: false,
    attention:true,
    req: false,
    show: false,
    typeShow: false,
    sendAct: false,
    isShare: false,
    tabActive: 0,
    id: '',
    detail: {},
    current: 0,
    applyShow: false,
    permission: {},
    permission1: false,
    apply: {},
    userInfo: {},
    pageEnd: false,
    param: {
      pageNumber: 1,
      pageSize: 10
    },
    list: [],
    loginShow: false,
    login: false,
    phone: false,
    phoneshow: false,
    wishShow: false,
    addbtn:false,
    iptShow: false,
    isSend: false,
    keyheight: 0,
    content:"",
    isbirthdayshow: false,
    birthdaytoday:'',
    todayUserBirthday:[]
  },
  login() {
    if (this.data.login) {
      this.toggle('phoneshow', true)
      this.selectComponent('#phone').showClose()
    } else {
      this.toggle('loginShow', true)
      this.selectComponent('#login').showClose()
    }
  },
  loginClose() {
    this.toggle('loginShow', false)
    this.toggle('isbirthdayshow',false)
  },
  toggleToast(e) {
    console.log(e.detail)
    if (!e.detail.loginShow) {
      this.toggle('loginShow', false)
    }
    this.refreshData()
  },
  toggleToast1(e) {
    console.log(e.detail)
    if (!e.detail.phoneshow) {
      this.toggle('phoneshow', false)
    }
    this.refreshData()
  },
  user(e) {
    var that = this;
    var id = e.currentTarget.dataset.createuid;
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/myCardNew/myCardNew?type=visitor&id=' + id + '&name=' + name,
    })
  },
  org(e) {
    console.log('org')
    var dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/orgHome/orgHome?id=' + dataset.orgid
    })
  },
  //生日弹框选择
  birthdaychack(e){
    var i = e.currentTarget.dataset.index
    var arr = this.data.todayUserBirthday
    console.log(arr)
    arr[i].isSelect = !arr[i].isSelect
    this.setData({
      todayUserBirthday:arr
    })
  },
  //送祝福
  giveblessing(){
    var that = this
    var list=[]
    for (let b of this.data.todayUserBirthday){
      if (b.isSelect){
        var c ={
          'blessActId': b.birthdayId,
          'receiveId': b.userId,
          'blessActType':1,
          'content':'生日快乐'
        }
        list.push(c)
      }
    }
    console.log('祝福列表',list)
    if(list.length>0){
      app.req({
        url: "/orgmini/org/saves",
        data: {
          list: list
        },
        success: function (res) {
          if (200 == res.code) {
            that.setData({
              isbirthdayshow: false
            })
            wx.showToast({
              title: '已为您送上对TA的生日祝福',
              icon:'none'
            })
            setTimeout(() => {
              wx.navigateTo({
                url: '/pages/birthday/birthday?id=' + that.data.detail.id
              })
            }, 1000)
          }
        }
      });
    }else{
      wx.showToast({
        title: '请选择人员',
        icon: 'none'
      })
    }
  },
  sendActivity() {
    this.toggle("typeShow", true)
    this.toggle("sendAct", false)
    this.toggle("manage", false);
  },
  showType() {
    this.toggle("typeShow", true)
    this.toggle("addbtn", true)
  },
  openManage(){
    this.toggle("manage", true);
  },
  typeClose() {
    this.toggle("typeShow", false);
    this.toggle("wishShow", false);
    this.toggle("sendAct", false);
    this.toggle("addbtn", false);
    this.toggle("manage", false)
  },
  addAttention(){
    var that = this
    var a = 'a'
    if (that.data.attention){
      this.toggle("attention", false);
      app.req({
        noLoading: true,
        url: "orgmini/attention/addAttention",
        data: {
          orgId: that.data.id
        },
        success: function (res) {
          if (200 == res.code) {
            wx.showToast({
              title: '您已成功关注此组织',
              duration: 3000,
              icon: 'none'
            })
            that.detail(a)
            app.getRefresh(function (refresh) {
            }, '1')
          }
        }
      });
    }
  },
  cancelAttention(){
    var that = this
    var a = 'a'
    if (that.data.attention) {
    this.toggle("attention", false);
    app.req({
      noLoading:true,
      url: "orgmini/attention/cancelAttention",
      data: {
        orgId: that.data.id
      },
      success: function (res) {
        if (200 == res.code) {
          wx.showToast({
            title: '成功取消关注',
            duration: 3000,
            icon: 'none'
          })
          app.getRefresh(function (refresh) {
          }, '1');
          that.detail(a)
        }
      }
    });
    }
  },
  activityDetail: function(e) {
    var that = this;
    if (that.data.detail.isOrg == 2 || that.data.detail.isOrg == 3) {
      console.log("组织外人员查看活动详情")
    }
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    var actType = e.currentTarget.dataset.acttype;
    var url = "";
    var isOrg = that.data.detail.isOrg == 2 || that.data.detail.isOrg == 3;
    console.log(isOrg)
    if (type == 1) {
      url = '/pages/register/register?id=' + id;
    } else if (type == 2 && actType != 1) {
      url = '/pages/hundredDetailed/hundredDetailed?id=' + id + "&type=" + actType;
    } else if (type == 2 && actType == 1) {
      url = '/pages/ordinaryDetails/ordinaryDetails?id=' + id;
    }
    wx.navigateToMiniProgram({
      appId: type == 1 ? app.globalData.activityMiniAppId : app.globalData.blessingMiniAppId,
      envVersion: type == 1 ? app.globalData.activityMiniEnvVersion : app.globalData.blessingMiniEnvVersion,
      path: url,
      success(res) {
        console.log(res);
      }
    })
  },
  toMyActivity: function(e) {
    var dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/packageA/pages/myActivity/myActivity?id=' + dataset.id + '&userInfoId=' + this.data.userInfo.id
    })
  },
  toDetail: function(e) {
    var dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/resourceList/resourceList?id=' + dataset.id + "&type=" + dataset.type,
    })
  },
  toheat: function(e) {
    var dataset = e.currentTarget.dataset;
    getApp().mtj.trackEvent('yqlw_org_heat');
    wx.navigateTo({
      url: '/pages/heat/heat?id=' + dataset.id
    })
  },
  tobirthday(e) {
    this.toggle("manage",false);
    var dataset = e.currentTarget.dataset;
    var limit = this.data.permission.isAdmin || this.data.permission.hasItem == true;
    if (dataset.type==1){
      getApp().mtj.trackEvent('yqlw_org_birth');
    }else{
      getApp().mtj.trackEvent('yqlw_org_next_birth');
    }
    wx.navigateTo({
      url: '/pages/birthday/birthday?id=' + dataset.id + "&limit=" + limit
    })
  },
  check: function(e) {
    var type = e.currentTarget.dataset.type
    if (type==1){
      getApp().mtj.trackEvent('yqlw_manage_notice');
    }else if(type==2){
      getApp().mtj.trackEvent('yqlw_manage_mation');
    } else if (type == 3) {
      getApp().mtj.trackEvent('yqlw_manage_resource');
    } else if (type == 4) {
      getApp().mtj.trackEvent('yqlw_manage_building');
    } else if (type == 5) {
      getApp().mtj.trackEvent('yqlw_org_information');
    } else if (type == 6) {
      getApp().mtj.trackEvent('yqlw_org_notice');
    } else if (type == 7) {
      getApp().mtj.trackEvent('yqlw_org_resources');
    }else if(type == 8){
      getApp().mtj.trackEvent('yqlw_org_auth_click');
    }
    if (type == 8 && this.data.detail.authStatus == 1) {
      return
    }
    this.toggle("show", true);
    this.toggle("manage", false);
  },
  onClose: function() {
    this.toggle("show", false);
  },
  contact: function(e) {
    console.log(e);
  },
  sendAct() {
    this.toggle("sendAct", true);
  },
  onChange(e) {
    var that = this
    var index = e.currentTarget.dataset.index;
    if (index==1){
      getApp().mtj.trackEvent('yqlw_tab_information');
    } else if (index == 2){
      getApp().mtj.trackEvent('yqlw_org_tab_notice');
    } else if (index == 3) {
      getApp().mtj.trackEvent('yqlw_tab_resources');
    } else if (index == 4) {
      getApp().mtj.trackEvent('yqlw_tab_building');
    }
    if (index != that.data.tabActive) {
      that.toggle("list", []);
      that.toggle('tabActive', index);
      that.toggle("param.pageNumber", 1);
      that.toggle("pageEnd", false);
      if (index == 0) {
        that.activityList()
      } else {
        that.otherList()
      }
    }
  },
  bindchange(e) {
    var current = e.detail.current
    this.toggle('current', current)
  },
  applyShow() {
    // this.toggle('applyShow', true)
    // 加入组织
    wx.navigateTo({
      url: '/pages/joinOrg/joinOrg?orgId=' + this.data.orgId,
    })
  },
  applyClose() {
    this.toggle('applyShow', false)
  },
  imgpopup(e) {
    var that = this
    var type = e.currentTarget.dataset.type
    if (type == 0) {
      that.toggle('imgpopup', true);
    } else {
      that.toggle('imgpopup', false);
    }
  },
  // 更换封面
  changeBg: function(e) {
    var that = this;
    var type = e.currentTarget.dataset.type
    if (type == 0) {
      wx.navigateTo({
        url: '/pages/gallery/gallery?id=' + that.data.id,
      })
    } else if (type == 1) {
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['camera'],
        success(res) {
          // that.saveData();
          const avatar = res.tempFilePaths[0];
          wx.navigateTo({
            url: '../upload/upload?src=' + avatar + '&id=' + that.data.id
          })
        }
      })
    }else if (type == 2) {
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album'],
        success(res) {
          // that.saveData();
          const avatar = res.tempFilePaths[0];
          wx.navigateTo({
            url: '../upload/upload?src=' + avatar + '&id=' + that.data.id
          })
        }
      })
    }
  },

  onParamChange: function(e) {
    var value = e.detail.value;
    var param = e.currentTarget.dataset.param;
    this.toggle(param, value);
  },
  validateApply: function() {
    var config = {
      "applyName": "姓名",
      "applyTel": "手机号"
    };
    var that = this;
    var data = that.data.apply;
    that.toggle('apply.applyTel', that.data.apply.applyTel.replace(/ /g, ''))
    for (let i in config) {
      if (!data[i]) {
        wx.showToast({
          title: config[i] + '不能为空',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
    }
    if (!(/^1[3456789]\d{9}$/.test(data.applyTel))) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    return true;
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  birthday: function(e) {

  },
  mini: function(e) {
    var that = this;
    var d = e.currentTarget.dataset;
    if (d.type == 13) {
      this.toggle("typeShow", false)
      this.toggle("wishShow", true)
      return
    }
    if (d.type == 4) {
      if (this.data.addbtn){
        getApp().mtj.trackEvent('yqlw_org_act_banquet');
      }else{
        getApp().mtj.trackEvent('yqlw_manage_banquet');
      }
    } else if (d.type == 3) {
      if (this.data.addbtn) {
        getApp().mtj.trackEvent('yqlw_org_act_sports');
      }else{
        getApp().mtj.trackEvent('yqlw_manage_sports');
      }
    } else if (d.type == 8) {
      if (this.data.addbtn) {
        getApp().mtj.trackEvent('yqlw_org_act_meeting');
      } else {
        getApp().mtj.trackEvent('yqlw_manage_meeting');
      }
    } else if (d.type == 12) {
      if (this.data.addbtn) {
        getApp().mtj.trackEvent('yqlw_org_act_birth');
      } else {
        getApp().mtj.trackEvent('yqlw_manage_birth');
      }
    } else if (d.type == 14) {
      if (this.data.addbtn) {
        getApp().mtj.trackEvent('yqlw_org_act_hundred');
      } else {
        getApp().mtj.trackEvent('yqlw_manage_hundred');
      }
    } else if (d.type == 15) {
      if (this.data.addbtn) {
        getApp().mtj.trackEvent('yqlw_org_act_banquet');
      } else {
        getApp().mtj.trackEvent('yqlw_manage_banquet');
      }
    }
    var url = "";
    if (d.turn) {
      url = d.turn
    } else {
      url = d.type == 12 ? '/pages/sport/sport' : '/pages/party/party';
    }
    url += "?type=" + d.type + "&name=" + d.name;
    console.log("组织url", url)

    wx.navigateToMiniProgram({
      appId: !Boolean(d.turn) ? app.globalData.activityMiniAppId : app.globalData.blessingMiniAppId,
      envVersion: !Boolean(d.turn) ? app.globalData.activityMiniEnvVersion : app.globalData.blessingMiniEnvVersion,
      path: url,
      success(res) {
        that.toggle("typeShow", false);
        that.toggle("addbtn", false)
      }
    })
  },
  totoast(){
    wx.showModal({
      title: '温馨提示',
      content: '管理员还未配置好打卡内容，请通知管理员或稍后再试',
      showCancel:false,
      confirmColor:'#2168eb'
    })
  },
  torelease(){
    wx.navigateTo({
      url: "/pages/orgRelease/orgRelease?id=" + this.data.detail.id + '&orgSearch=' + this.data.detail.orgSearch
    })
    this.toggle("typeShow",false)
  },
  tohf(e){
    var dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: "/pages/duesDetails/duesDetails?id=" + dataset.id
    })
  },
  toPage(e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    if (dataset.type && dataset.band) {
      wx.showToast({
        title: '加入组织后才可查看',
        icon: 'none',
        duration: 2000
      });
      return
    }
    var url = dataset.turn;
    if (url =='/packageA/pages/orgDetails/orgDetails'){
      getApp().mtj.trackEvent('yqlw_org_brief');
    } else if (url =='/pages/memberList/memberList'){
      getApp().mtj.trackEvent('yqlw_org_member');
    } else if (url == '/pages/orgAccount/orgAccount') {
      getApp().mtj.trackEvent('yqlw_org_account');
    } else if (url =='/pages/group/group'){
      getApp().mtj.trackEvent('yqlw_org_group');
    } else if (url =='/pages/setManager/setManager'){
      getApp().mtj.trackEvent('yqlw_org_manager');
    }
    if (dataset.id && dataset.orgid) {
      url = url + "?id=" + dataset.id + "&orgId=" + that.data.orgId + "&isOrg=" + dataset.band;
    } else if (dataset.id && !dataset.orgid) {
      url += "?id=" + dataset.id + "&isOrg=" + dataset.band + "&orgname=" + that.data.detail.orgName + '&set=' + that.data.detail.isSetup + "&origin=" + dataset.origin + "&type=" + that.data.detail.orgType;
    } else if (!dataset.id && dataset.orgid) {
      url += "?orgid=" + dataset.orgid + "&orgname=" + dataset.name + "&orgimg=" + dataset.img + "&isOrg=" + dataset.band;
    }

    if (dataset.turn == '/pages/newIndex/newIndex'){
      wx.switchTab({
        url: url
      })
    }else{
      wx.navigateTo({
        url: url
      })
    }
    this.toggle("manage", false);
    this.toggle("bigImg", false);
    this.toggle("param.pageNumber", 1);
  },
  toSh(){
    wx.navigateTo({
      url: '/packageA/pages/orgIntroduce/orgIntroduce?id=' + this.data.detail.id
    })
  },
  toPage1(e) {
    var that = this
    var dataset = e.currentTarget.dataset;
    getApp().mtj.trackEvent('yqlw_org_structure');
    if (dataset.band) {
      return;
    }
    var url = dataset.turn + "?orgid=" + dataset.orgid + "&orgname=" + dataset.name + "&orgimg=" + dataset.img
    console.log(url)
    wx.navigateTo({
      url: url
    })
    this.toggle("manage", false);
  },
  toPage2(e){
    var that = this;
    var managerAuthList = false
    for (let i of this.data.detail.managerAuthList) {
      if (i == 15) {
        managerAuthList = true
      }
    }
    var url = e.currentTarget.dataset.turn + "?id=" + this.data.detail.id + '&isOrg=' + this.data.detail.isOrg + "&managerAuthList=" + managerAuthList;
    wx.navigateTo({
      url: url
    })
  },
  tohflist(){
    var managerAuthList =false
    for (let i of this.data.detail.managerAuthList){
      if(i==15){
        managerAuthList=true
      }
    }
    wx.navigateTo({
      url: "/pages/hfjnList/hfjnList?id=" + this.data.detail.id + '&type=1' + '&isOrg=' + this.data.detail.isOrg + "&managerAuthList=" + managerAuthList
    })
  },
  tostatistics(){
    wx.navigateTo({
      url: "/pages/statistics/statistics?orgid=" + this.data.detail.id + "&isorg=1" + '&orgname=' + this.data.detail.orgName + '&orgurl=' + this.data.detail.orgUrl
    })
  },
  towork(){
    wx.navigateTo({
      url: '/packageA/pages/workStatistics/workStatistics?orgid=' + this.data.detail.id + "&isorg=1" + ' &orgname=' + this.data.detail.orgName + '&orgurl=' + this.data.detail.orgUrl + '&isParentOrgRw=' + this.data.isParentOrgRw + '&isAdministrators=' + this.data.detail.isOrg,
    })
  },
  toParty() {
    wx.navigateTo({
      url: '/pages/partyBuilding/partyBuilding?id=' + this.data.detail.id        + "&isorg=1" + ' &orgname=' + this.data.detail.orgName + '&orgurl=' + this.data.detail.orgUrl + '&isParentOrgRw=' + this.data.isParentOrgRw + '&isAdministrators=' + this.data.detail.isOrg,
    })
  },
  mini1: function (e) {
    var that = this;
    var d = e.currentTarget.dataset;
    if (!d.item.birthday) {
      return
    }
    var url = '/pages/ordinaryDetails/ordinaryDetails?id=' + d.id;
    wx.navigateToMiniProgram({
      appId: app.globalData.blessingMiniAppId,
      envVersion: app.globalData.blessingMiniEnvVersion,
      path: url,
      success(res) {
        // that.toggle("typeShow", false);
      }
    })
  },
  mini: function (e) {
    var that = this;
    var d = e.currentTarget.dataset;
    if (d.type == 13) {
      this.toggle("typeShow", false)
      this.toggle("wishShow", true)
      return
    }
    if (d.type == 4) {
      getApp().mtj.trackEvent('yqlw_act_banquet');
    } else if (d.type == 3) {
      getApp().mtj.trackEvent('yqlw_act_sports');
    } else if (d.type == 8) {
      getApp().mtj.trackEvent('yqlw_act_meeting');
    } else if (d.type == 12) {
      getApp().mtj.trackEvent('yqlw_act_birth');
    } else if (d.type == 14) {
      getApp().mtj.trackEvent('yqlw_act_hundred');
    } else if (d.type == 15) {
      getApp().mtj.trackEvent('yqlw_act_banquet');
    }
    var url = "";
    if (d.turn) {
      url = d.turn
    } else {
      url = d.type == 12 ? 'pages/sport/sport' : 'pages/party/party';
    }
    url += "?type=" + d.type + "&name=" + d.name;
    // console.log("组织url", url)

    wx.navigateToMiniProgram({
      appId: !Boolean(d.turn) ? app.globalData.activityMiniAppId : app.globalData.blessingMiniAppId,
      envVersion: !Boolean(d.turn) ? app.globalData.activityMiniEnvVersion : app.globalData.blessingMiniEnvVersion,
      path: url,
      success(res) {
        that.toggle("typeShow", false);
      }
    })
  },
  detail: function(a) {
    var that = this;
    if(a=='a'){
      var noLoading = true
    }else{
      var noLoading = false
    }
    app.req({
      // url: "orgmini/org/detail",
      noLoading: noLoading,
      url: "orgmini/org/newGetOrgInfo",
      data: {
        orgId: that.data.id
      },
      success: function(res) {
        that.getMember();
        if (200 == res.code) {
          that.toggle("todayUserBirthday", res.data.todayUserBirthday);
          for (var a in that.data.todayUserBirthday) {
            var sexParam = "todayUserBirthday[" + a + "].isSelect"
            var birblessing = "todayUserBirthday[" + a + "].blessing"
            var index = Math.floor(Math.random() * 3);
            var s = ''
            if(index==0){
             s='今天是TA的生日'
            } else if(index == 1){
              s = 'Happy Birthday'
            } else if (index == 2) {
              s = '为TA送上祝福吧'
            }
            that.setData({
              [sexParam]: false,
              [birblessing]: s,
            })
          }
          if (res.data.toDayJoinOrgNumber == 1 && res.data.todayUserBirthday.length>0 &&( res.data.isOrg == 0 || res.data.isOrg == 1 || res.data.isOrg == 4 || res.data.isOrg == 5)){
            that.toggle("isbirthdayshow", true);
          }
          var today = util.itemDateToShow(new Date(),1,false,false).substring(5);
          that.toggle("birthdaytoday", today);
          var detail = res.data
          that.toggle("detail", detail);
          that.toggle("tabActive", detail.location);
          that.toggle('isParentOrgRw', detail.isParentOrgRw)
          that.toggle("attention", true);
          if(a!='a'){
            if (that.data.tabActive == 0) {
              that.activityList();
            } else if (that.data.tabActive != 0) {
              that.otherList();
            }
          }
          var permission = {};
          var detail = res.data;
          permission.isAdmin = detail.isOrg * 1 == 5 || (detail.isOrg * 1 == 1 && detail.isAdmin == 1);
          permission.isManager = detail.isOrg * 1 == 5;
          var userPermission = res.data.managerAuthList;
          // console.log('管理员权限列表', userPermission)
          if (userPermission && userPermission.length > 0) {
            for (let i of userPermission) {
              // console.log(i);
              permission[i] = 1;
              if (userPermission[i] == 11) {
                that.toggle("permission1", true)
              }
            }
            permission.hasItem = 1;
          }
          that.toggle("permission", permission);
          if (detail.birthdayUerList && 0 != detail.birthdayUerList.length) {
            for (let i of detail.birthdayUerList) {
              var date = util.toDate(i.birthday);
              var month = date.getMonth() + 1;
              var day = date.getDate();
              i.birthdayShow = month + "月" + day + "日";
            }
          }
          if (detail.birthdayUerList.length){
            var arr = detail.birthdayUerList
            var list =[]
            var num = parseInt(arr.length % 3 )? (parseInt(arr.length / 3) + 1) : parseInt(arr.length / 3)
            // console.log("生ri ", num)
            for(var i =0;i<num;i++){
              var a=[]
              for(var j =0;j<3;j++){
                if(arr[0]){
                  a.push(arr[0])
                  arr.splice(0, 1);
                }
              }
              list.push(a)
            }
            that.toggle("detail.birthdayUerList", list)
            // console.log("生日列表", list)
          }
          if (res.data.isDissolution == 1){
            wx.showModal({
              title: '',
              content: '此组织已解散',
              showCancel:false,
              success(res) {
                if (res.confirm) {
                  if (that.data.mycard==1){
                    wx.navigateBack({
                    })
                  }else{
                    wx.switchTab({
                      url: '/pages/newIndex/newIndex'
                    })
                  }
                }
              }
            })
          }
        }
      }
    });
  },
  activityList: function(append) {
    var that = this;
    that.toggle("req", false);
    app.req({
      url: "orgmini/act/orgActivityList",
      data: {
        "orgId": that.data.param.orgId,
        "type": 0,
        "pageNumber": that.data.param.pageNumber,
        "pageSize": that.data.param.pageSize
      },
      success: function(res) {
        if (200 == res.code) {
          var list = res.data.page.rows
          for (var item of list) {
            if (item.createTime) {
             
                item.createTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16)
             
            }
            if (item.applyEndTime) {
              var applyEndTimeStamp = new Date(item.applyEndTime.replace(/-/g, "/")).getTime();
              var timestamp = new Date().getTime();
              if (applyEndTimeStamp < timestamp) {
                item.endApply = true;
              } else {
                item.endApply = false;
              }
            } else {
              item.endApply = false;
            }
          }
          for (var item of list) {
            if (item.actStartTime.substring(0, 10) == item.actEndTime.substring(0, 10)) {
              item.actTime = item.actStartTime.substring(5, 7) + '月' + item.actStartTime.substring(8, 10) + '日' + item.actStartTime.substring(11, 16) + '-' + item.actEndTime.substring(11, 16)
            } else {
              item.actTime = item.actStartTime.substring(5, 7) + '月' + item.actStartTime.substring(8, 10) + '日' + item.actStartTime.substring(11, 16) + '-' + item.actEndTime.substring(5, 7) + '月' + item.actEndTime.substring(8, 10) + '日' + item.actEndTime.substring(11, 16)
            }
          }
          if (!list || list.length < that.data.param.pageSize) {
            that.toggle("pageEnd", true);
          }
          that.toggle("list", append ? that.data.list.concat(list) : list);
          that.toggle("req", true);
        }
      }
    });
  },
  otherList: function(append) {
    var that = this;
    that.toggle("req", false);
    if (that.data.tabActive == 1) {
      var messageType = 3
    } else if (that.data.tabActive == 2) {
      var messageType = 1
    } else if (that.data.tabActive == 3) {
      var messageType = 4
    } else if (that.data.tabActive == 4) {
      var messageType = 4
    } else if (that.data.tabActive == 5) {
      var messageType = 5
    }
    var url = '';
    var data = {};
    if (that.data.tabActive != 4 && that.data.tabActive != 5) { 
      url = "orgmini/org/list";
      data = {
        "orgId": that.data.param.orgId,
        "messageType": messageType,
        "pageNumber": that.data.param.pageNumber,
        "pageSize": that.data.param.pageSize
      }
    } else if (that.data.tabActive == 5){
      url = "orgmini/business/list";
      data = {
        "entityId": that.data.detail.digitalCommerce,
        "pageNumber": that.data.param.pageNumber,
        "pageSize": that.data.param.pageSize,
      }
    }else{
      url = "orgmini/orgMessage/memberList";
      data = {
        "orgId": that.data.param.orgId,
        "pageNumber": that.data.param.pageNumber,
        "pageSize": that.data.param.pageSize,
      }
    }
    // 商机
    if (this.data.tabActive == 5){
      app.req({
        url: url,
        data: data,
        success: function (res) {
          if (200 == res.code) {
            var list = res.data
            for (var i = 0; i < list.length; i++) {
              list[i].width = "";
              let index = i;
              if (list[index].attachments && list[index].attachments.length == 1) {
                wx.getImageInfo({
                  src: list[index].attachments[0].url,
                  complete: (res) => {
                    console.log('图片信息',res)
                    var scale = res.width / res.height;
                    var height = parseFloat(500 / scale).toFixed(2);
                    that.setData({
                      ['list[' + index + '].height']: height
                    });
                    if (scale > 1) {
                      that.setData({
                        ['list[' + index +'].width']: 1
                      });
                    } else {
                      that.setData({
                        ['list[' + index + '].width']: 0
                      });
                    }
                  }
                })
              } else {
                // that.toggle("list", append ? that.data.list.concat(list) : list);
              }
            }
            that.toggle("list", append ? that.data.list.concat(list) : list);
            if (!list || list.length < that.data.param.pageSize) {
              that.toggle("pageEnd", true);
            }
            that.toggle("req", true);
          }
        }
      });
    }else{
      app.req({
        url: url,
        data: data,
        success: function(res) {
          if (200 == res.code) {
            var list = res.data.rows
            for (var item of list) {
              if (item.createTime) {
                  item.createTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16)
              }
              if (item.commentDto && item.commentDto.length > 3){
                item.dto = item.commentDto.slice(0,3);
              }else{
                item.dto = item.commentDto;
              }
              item.width = "";
            }
            for (var i = 0; i < list.length; i++){
              let index = i;
              if (list[index].imgs && list[index].imgs.length ==1){
                wx.getImageInfo({
                  src: list[index].imgs[0].fileUrl,
                  complete: (res) => {
                    var scale = res.width / res.height;
                    var height = parseFloat(500 / scale).toFixed(2);
                    that.setData({
                      ['list[' + index + '].height']: height
                    });
                    if (scale > 1) {
                      that.setData({
                        ['list[' + index + '].width']: 1
                      });
                    }else{
                      that.setData({
                        ['list[' + index + '].width']: 0
                      });
                    }
                    // if (that.data.tabActive == 3) {
                    //   that.toggle("list", append ? that.data.list.concat(list) : list);
                    // }
                  }
                })
              }
            }
            if (!list || list.length < that.data.param.pageSize) {
              that.toggle("pageEnd", true);
            }
            that.toggle("list", append ? that.data.list.concat(list) : list);
            that.toggle("req", true);
          }
        }
      });
    }
  },
  // 分享
  hidden() {
    this.onShareAppMessage();
  },
  // 组织删除
  orgDelete(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '请确认是否删除',
      confirmText: "确认",
      success(res) {
        if (res.confirm) {
          var type = e.currentTarget.dataset.type;
          var actType = e.currentTarget.dataset.acttype;
          var relationId = e.currentTarget.dataset.id;
          var messageType = "";
          if (type == 1){
            messageType = 0;
          } else if (type == 2 && actType == 2){
            messageType = 5;
          } else if (type == 2 && actType == 3) {
            messageType = 6;
          } else if (type == 3) {
            messageType = 2;
          } else if (type == 4) {
            messageType = 1;
          } else if (type == 5) {
            messageType = 2;
          } else if (type == 6) {
            messageType = 4;
          } else if (type == 7) {
            messageType = 7;
          }
          if (that.data.tabActive == 5){
            var url = 'orgmini/business/deleteActivity';
            var data = {
              "id": relationId,
            }
          }else{
            var url = "orgmini/activity/orgDeleteAct";
            var data = {
              relationId: relationId,
              type: messageType,
              orgId: that.data.orgId
            }
          }
          app.req({
            url: url,
            data: data,
            success: function (res) {
              if (200 == res.code) {
                if (type == 1 || type == 2 ){
                  that.activityList();
                } else{
                  that.toggle("param.pageNumber",1);
                  that.otherList();
                }
                wx.showToast({
                  title: '删除成功',
                  icon: 'none'
                });
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
          var index = e.currentTarget.dataset.index;
          var temp_str = 'list[' + index + '].isMore';
          that.setData({
            [temp_str]: false
          })
        }
      }
    })
  },
  getMember: function () {
    var that = this;
    that.toggle('req', false);
    app.req({
      noLoading: true,
      url: "orgmini/org/newOrgUserPage",
      data: {
        orgId: that.data.id,
        pageNumber: 1,
        pageSize: 3,
      },
      success: function (res) {
        if (200 == res.code) {
          var newList = res.data.page.rows;
          that.toggle("userUrlList", newList);
          that.toggle('req', true)
        }
      }
    });
  },
  toUserCard: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/myCardNew/myCardNew?type=visitor&id=' + id + '&name=' + name,
    })
  },

  preventTouchMove: function (e) {
  },
  isorghide(e) {
    var index = e.currentTarget.dataset.index;
    var temp_str = 'list[' + index + '].isMore';
    this.setData({
      [temp_str]: false
    })
  },
  // 打开更多操作
  setMore(e) {
    var index = e.currentTarget.dataset.index;
    var temp_str = 'list[' + index + '].isMore';
    this.setData({
      [temp_str]: true
    })
  },
  openImg(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = e.currentTarget.dataset.item;
    var remark = e.currentTarget.dataset.remark;
    var imgArr = [];
    if (remark){
      imgArr = remark;
    }else{
      for (var i = 0; i < item.length; i++) {
        if (this.data.tabActive == 5){
          imgArr.push(item[i].url);
        }else{
          imgArr.push(item[i].fileUrl);
        }
      };
    }
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) {
        that.toggle("bigImg", true);
      },
    })
  },

  refresh() {
    var that = this
    console.log('数据', that.data.options)
    that.onLoad(that.data.options)
    that.onShow()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.toggle('options', options)
    wx.getNetworkType({
      success(res) {
        const networkType = res.networkType
        console.log('网络', networkType)
        if (networkType == 'none') {
          that.toggle('iswifi', true)
        } else {
          that.toggle('iswifi', false)
        }
      }
    })
    // var id = '06b0692609d711ea995200163e05ff66';
    // console.log("orgId:" + options.id);
    that.toggle("id", options.id || options.scene);
    that.toggle("orgId", options.id);
    that.toggle("apply.orgId", options.id);
    that.toggle("param.orgId", options.id);
    if (options.mycard){
      that.toggle("mycard", options.mycard);
    }
    // this.toggle("id", id);
    // this.toggle("orgId", id);
    // this.toggle("apply.orgId", id);
    // this.toggle("param.orgId", id);
  },
// getScrollTop:function(){
//   var that = this
//   var query = wx.createSelectorQuery()
//   query.select('#tabsid').boundingClientRect()
//   // query.selectViewport.scrollOffset()
//   query.exec(function(res){
//     console.log('距离', res[0].top)
//    that.setData({
//      top: res[0].top
//    })
//   })
// },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var that = this
    // setTimeout(function () {
    
    // }, 2000)
  },
  refreshData: function() {
    var that = this;
    wx.getSetting({
      success(res) {
        // if (!res.authSetting['scope.userInfo'] || wx.getStorageSync('uid') == '') {
        if ( wx.getStorageSync('uid') == '') {
          that.toggle("login", false);
          that.toggle("apply.remake", '');
          that.toggle("apply.applyName", '');
          that.toggle("apply.applyTel", '');
          that.detail();
        } else {
          that.toggle("login", true);
          var phone = wx.getStorageSync("phone")
          if (phone == '') {
            that.toggle("phone", true);
          } else {
            that.toggle("phone", false);
          }
          app.getUserInfo(function(userInfo) {
            that.toggle("userInfo", userInfo);
            that.toggle("apply.uid", userInfo.id);
            that.toggle("apply.remake", '');
            that.toggle("apply.applyName", userInfo.realName);
            that.toggle("apply.applyTel", userInfo.telphone);
          });
          if (!that.data.bigImg) {
            that.detail();
          }
        }
      },
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // this.getlistFriend();
    // this.setData({
    //   top: 0,
    //   fixedNav: false
    // })
    this.toggle('isShare', false);
    this.toggle("sendAct", false);
    this.toggle("imgpopup", false);
    this.refreshData();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.toggle('isShare', false);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.toggle("param.pageNumber", 1);
    this.refreshData();
    this.detail()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    // console.log("加载下一页", !that.data.pageEnd)
    if (!that.data.pageEnd) {
      that.toggle("param.pageNumber", that.data.param.pageNumber + 1);
      if (that.data.tabActive == 0) {
        that.activityList(true);
      } else if (that.data.tabActive != 0) {
        that.otherList(true);
      }
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    this.toggle("manage", false);
    this.toggle("isShare",true);
    var title='';
    // if (res.target && res.target.dataset.type==1){
    //   title= this.data.userInfo.realName+'给你推荐了一个特别棒的组织'
    // }else{
      getApp().mtj.trackEvent('yqlw_org_join');
      title= this.data.userInfo.realName + "邀请您加入" + this.data.detail.orgName
    // }
    return {
      title: title
    }　


  },
  // onPageScroll: function (res) {
  //   if (res.scrollTop >= this.data.top){
  //    this.setData({
  //      fixedNav:true
  //    })
  //  }else{
  //    this.setData({
  //      fixedNav: false
  //    })
  //  }
  // },
})